package com.easy.controller;

import com.easy.bean.Order;
import com.easy.common.CommonResult;
import com.easy.service.IOrderService;
import com.easy.service.IPrivateService;
import com.easy.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@RestController
public class OrderController {

    @Autowired
    IOrderService orderS;

    @Autowired
    IPrivateService privateS;

    @PostMapping("order")
    public CommonResult addOrder(@RequestBody Order order){
        orderS.addOrder(order);
        int res = privateS.editPrivateClassTotal(order);
        if(res == 0){
            privateS.addPrivateClass(order);
        }
        return CommonResult.success();
    }

    @GetMapping("order")
    public CommonResult getOrder(Order order, Page page){
        List<Order> list = orderS.getOrder(order,page);
        int count = orderS.getOrderCount(order);
        HashMap map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return CommonResult.success(map);
    }

    @GetMapping("trainerchart")
    public CommonResult trainerChart(){
        List<Order> list = orderS.trainerChart();
        System.out.println(list);
        //循环
        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        List<Integer> yAxis2 = new ArrayList<>();
        for(Order o : list){
            xAxis.add(o.getTrainerName());
            yAxis.add(o.getPayment());
            yAxis2.add(o.getTotal());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("xAxis",xAxis);
        map.put("yAxis",yAxis);
        map.put("yAxis2",yAxis2);
        return CommonResult.success(map);
    }

}
